// index.js
// 获取应用实例
const app = getApp()
const api = require('../../api/index.js')
const viewUrl = app.globalData.viewUrl
Page({
  data: {
    url: '',
  },

  onLoad(options) {
    const self = this
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          api.login({
            code: res.code
          }).then((res) => {
            if (res.token) {
              const {openid} = res.user
              console.log('openid', openid)
              app.setOpenid(openid);
              self.setData({
                url: `${viewUrl}/index.html#/wxlogin/index?openid=${openid}&time=${new Date().getTime()}&page=${options.page}`
              })
            }else {
              wx.redirectTo({
                url: '/pages/auth/auth'
              })
            }
            
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    
  },


  //webview 组件的三个原生事件
  bindload() {
    console.log('webview load success')
  },
  binderror() {
    console.log('webview load error')
  },
  //网页向小程序 postMessage 时，会在特定时机（小程序后退、组件销毁、分享）触发并收到消息。e.detail = { data }，data是多次 postMessage 的参数组成的数组
  getMessage: function (res) {
    console.log("H5传递过来的参数", res.detail.data);
  },
  onShareAppMessage(){},
  onShareTimeline(){}




})